package com.lagou.wwyan.rpc.server.handler;

import com.lagou.wwyan.rpc.bean.RpcRequest;
import com.lagou.wwyan.rpc.bean.RpcResponse;
import com.lagou.wwyan.rpc.server.factory.RpcBeanFactory;
import com.lagou.wwyan.rpc.sirealizer.Serializer;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;

import java.lang.reflect.Method;

public class RpcServerHandler extends SimpleChannelInboundHandler<RpcRequest> {
    private RpcBeanFactory beanFactory;

    public RpcServerHandler(RpcBeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcRequest msg) throws Exception {
        RpcRequest rpcRequest =  msg;
        System.out.println("服务端接收到请求："+rpcRequest.getRequestId());
        String className = rpcRequest.getClassName();
        Class<?> aClass = Class.forName(className);
        Object bean = beanFactory.getBean(aClass);
        Method method = null;
        try {
            method = aClass.getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
            Object result = method.invoke(bean, rpcRequest.getParameters());
            //组装返回数据
            RpcResponse response = new RpcResponse();
            response.setRequestId(rpcRequest.getRequestId());
            response.setClassName(method.getReturnType().getName());
            response.setData(result);
            //写入返回数据
            ctx.writeAndFlush(response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            //组装返回数据
            RpcResponse response = new RpcResponse();
            response.setRequestId(rpcRequest.getRequestId());
            response.setClassName(String.class.getName());
            response.setData("error");
            //写入返回数据
            ctx.writeAndFlush(response);
        }


    }

//    @Override
//    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        RpcRequest rpcRequest = (RpcRequest) msg;
//        System.out.println("服务端接收到请求："+rpcRequest.getRequestId());
//        String className = rpcRequest.getClassName();
//        Class<?> aClass = Class.forName(className);
//        Object bean = beanFactory.getBean(aClass);
//        Method method = null;
//        try {
//            method = aClass.getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
//            Object result = method.invoke(bean, rpcRequest.getParameters());
//            //组装返回数据
//            RpcResponse response = new RpcResponse();
//            response.setRequestId(rpcRequest.getRequestId());
//            response.setClassName(method.getReturnType().getName());
//            response.setData(result);
//            //写入返回数据
//            ctx.writeAndFlush(response).sync();
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//            //组装返回数据
//            RpcResponse response = new RpcResponse();
//            response.setRequestId(rpcRequest.getRequestId());
//            response.setClassName(String.class.getName());
//            response.setData("error");
//            //写入返回数据
//            ctx.writeAndFlush(response).sync();
//        }
//
//
//    }

}
